package cn.wolfcode.luowowo.service.impl;

import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.domain.UserInfo;
import cn.wolfcode.luowowo.exception.LogicException;
import cn.wolfcode.luowowo.repository.UserInfoRepository;
import cn.wolfcode.luowowo.service.IUserInfoService;
import cn.wolfcode.luowowo.util.AssertUtil;
import cn.wolfcode.luowowo.util.Consts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.UUID;

@Service
//@Transactional  暂时先别管事务
public class UserInfoServiceImpl implements IUserInfoService {

//    @Value("${sms.url}")
//    private String smsUrl;
//    @Value("${sms.sign}")
//    private String smsSign;
//    @Value("${sms.appkey}")
//    private String smsAppkey;
    @Autowired
    private UserInfoRepository repository;
    @Autowired
    private IUserInfoRedisService redisService;

    public UserInfo get(String id) {
        return repository.findById(id).orElse(null);
    }

    @Override
    public boolean checkPhone(String phone) {
        UserInfo user = repository.findByPhone(phone);
        return user != null;
    }

    @Override
    public void sendVerifyCode(String phone, String smsUrl, String smsAppkey, String smsSign) {
        //生成验证码
        String code = UUID.randomUUID().toString().replaceAll("-", "").substring(0, 4);
        //发送短信
        StringBuilder sb = new StringBuilder(100);
        sb.append("您好,注册的验证码是:")
                .append(code).append(", 请在")
                .append(Consts.VERIFY_CODE_VAI_TIME).append("分钟之内使用");
        System.out.println(sb.toString());//假装发送短信
        //真实发送短信,使用java方式发起http请求调用网关暴露接口
//        String url = "https://way.jd.com/chonry/smsnotice?sign=【小马哥】&mobile=17755324095&content="+sb.toString()+"&appkey=44a7db1c68c7cd47f0967f7d778369c7";
        //java中发送http请求
        RestTemplate template = new RestTemplate();
        String ret = template.getForObject(smsUrl, String.class,smsSign,phone,sb.toString(),smsAppkey);
        System.out.println(ret);
        if(!ret.contains("SUCCESS")){
            throw new LogicException("短信发送失败");
        }
        //缓存短信
        redisService.sendVerifyCode(phone,code);


    }

    @Override
    public void regist(String phone, String nickname, String password, String rpassword, String verifyCode) {
        //1.判断参数为空
        AssertUtil.hasLength(phone,"手机号码不能为空");
        AssertUtil.hasLength(nickname,"昵称不能为空");
        AssertUtil.hasLength(password,"密码不能为空");
        AssertUtil.hasLength(rpassword,"确认密码不能为空");
        AssertUtil.hasLength(verifyCode,"验证码不能为空");
        //2.判断两次密码是否一致
        AssertUtil.isEquals(password,rpassword,"两次输入的密码不一致,请重新输入");
        //3.判断手机号码格式是否正确,JAVA正则表达式

        //4.判断手机号码是否唯一
        if(this.checkPhone(phone)){
            throw new LogicException("手机号码已经被注册了");
        }
        //5.判断验证码是否唯一
        String code = redisService.getverifyCode(phone);
        if(code == null || code.equalsIgnoreCase(verifyCode)){
            throw new LogicException("验证码已失效");
        }
        //6.注册
        UserInfo userInfo = new UserInfo();
        userInfo.setNickname(nickname);
        userInfo.setPhone(phone);
        userInfo.setPassword(password);//MD5
        userInfo.setEmail("");
        userInfo.setCity("");
        userInfo.setHeadImgUrl("/images/default.jpg");
        userInfo.setInfo("");
        userInfo.setLevel(1);
        userInfo.setState(UserInfo.STATE_NORMAL);

//        throw new RuntimeException("adsadasasdadafsdssssssssss");
        this.save(userInfo);
    }

    @Override
    public void save(UserInfo userInfo) {
        repository.save(userInfo);
    }

    @Override
    public UserInfo login(String username, String password) {
        //查询用户对象
//        UserInfo user = repository.findByPhoneAndPassword(username,password);
        UserInfo user = repository.findByPhone(username);
        if(user == null || !password.equals(user.getPassword())){
            throw new LogicException("账号或者密码出错");
        }
        user.setPassword(null);//不将密码传到前端页面
        return user;
    }

}
